package model;

import util.DBUtil;

import java.util.List;
import java.util.Map;

public class FoodService {
    private DBUtil db = null;

    public FoodService() {
        db = new DBUtil();
    }

    /**
     * 获取热门菜
     *
     * @return
     */
    public List<Map<String, String>> getHotFoods() {
        //String sql="select id,foodname,price,picture from food order by hits Desc limit 0,3";
//        String sql="SELECT food.id,food.foodname,food.price,food.picture FROM food ORDER BY food.hits DESC LIMIT 0, 3";
        String sql = "select id,foodname,price,picture from food order by hits desc limit 0,3";

        return db.getList(sql);
    }

    /**
     * 获取特价菜
     *
     * @return List<Map < String, String>>
     */
    public List<Map<String, String>> getSpecialFoods() {
//        String sql="SELECT food.id,food.foodname,food.picture,food.comment FROM food where comment>0 ORDER BY food.hits DESC LIMIT 0, 3";
        String sql = "select id,foodname,picture,comment from food where comment>0 order by hits desc limit 0,3";

        return db.getList(sql);

    }

    /**
     * 获取厨师推荐
     *
     * @return
     */
    public List<Map<String, String>> getRecommFoods() {
//        String sql="SELECT food.id,food.foodname,food.price,food.picture FROM food where comment=0 ORDER BY food.hits DESC LIMIT 0, 3";
        String sql = "select id,foodname,price,picture from food where comment=0 order by hits desc limit 0,3";

        return db.getList(sql);
    }

    /**
     * 根据id 查找菜品的详细信息
     *
     * @return
     */

    public Map<String, String> getFood(String id) {
//        String sql="select f.*,ft.typename from food as f JOIN foodtype as ft on f.type=ft.id where f.id=?";
//        String[] params = {id};
//        return db.getMap(sql,params);
        String sql = "select f.*,ft.typename from food f join foodtype ft on f.type = ft.id where f.id=?";
        return db.getMap(sql, new String[]{id});
    }


    public List<Map<String, String>> getFoods(String s_fn, String s_type) {
        String sql = "select f.*,ft.typename from food f join foodtype ft on f.type = ft.id where 1=1";
        String[] params = null;
        if (s_fn != null && s_type != null) {
            sql = sql + " and foodname like ? and type like ?";
            params = new String[]{"%" + s_fn + "%", "%" + s_type + "%"};
        } else if (s_fn == null && s_type != null) {
            sql = sql + " and type like ?";
            params = new String[]{"%" + s_type + "%"};
        } else if (s_fn != null && s_type == null) {
            sql = sql + " and foodname like ?";
            params = new String[]{"%" + s_fn + "%"};
        }
        sql = sql + " order by hits desc, type asc, f.id asc";
        return db.getList(sql, params);
    }


    public Map<String, Object> getFoods(String s_fn, String s_type, String curPage) {
        String sql = "select f.*,ft.typename from food f join foodtype ft on f.type = ft.id where 1=1";
        String[] params = null;
        if (s_fn != null && s_type != null) {
            sql = sql + " and foodname like ? and type like ?";
            params = new String[]{"%" + s_fn + "%", "%" + s_type + "%"};
        } else if (s_fn == null && s_type != null) {
            sql = sql + " and type like ?";
            params = new String[]{"%" + s_type + "%"};
        } else if (s_fn != null && s_type == null) {
            sql = sql + " and foodname like ?";
            params = new String[]{"%" + s_fn + "%"};
        }
        sql = sql + " order by hits desc, type asc, f.id asc";
        if (curPage == null) {
            curPage = "1";
        }
        return db.getPage(sql, params, curPage);
    }

    public int editFood(String foodname, String feature, String material,
                        String price, String type, String picture, String comment, String id) {
        String sql = "update food set foodName=?,feature=?,material=?,price=?,type=?";
        if (picture != null) {
            sql = sql + ",picture=?";
        }
        sql = sql + ",comment=? where id=?";
        if (picture != null) {
            return db.update(sql, new String[]{foodname, feature,
                    material, price, type, picture, comment, id});
        } else {
            return db.update(sql, new String[]{foodname, feature,
                    material, price, type, comment, id});
        }
    }

    //删除菜品
    public int deleteFood(String id) {
        String sql = "delete from food where id=?";
        return db.update(sql, new String[]{id});
    }

    //添加菜品
    public int addFood(String fn, String fea, String mat, String price, String type, String picture, String comment) {
        String sql = "insert into food values(null,?,?,?,?,?,?,0,?)";
        return db.update(sql, new String[]{fn, fea, mat, price, type, picture, comment});
    }

//    public int editFood(String foodname, String price, String feature, String material) {

//    }

    //测试
    /*
    public static void main(String[] args) {
        FoodService fs = new FoodService();
        List<Map<String, String>> list = fs.getRecommFoods();
        for (Map<String, String> map : list) {
            System.out.println(map);
        }
    }
     */
}
